package com.example.demo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.demo.entity.Category;
import com.example.demo.service.ICategoryService;
import com.example.demo.utils.PageResult;
import com.example.demo.utils.PassToken;
import com.example.demo.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * categoryController
 * 
 * @author ruoyi
 * @date 2025-04-03
 */
@RestController
@RequestMapping("/category/category")
public class CategoryController
{
    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询category列表
     */
    @PassToken
    @GetMapping("/list")
    public ResponseResult list(Category category, @RequestParam(defaultValue = "1") int pageNum,  @RequestParam(defaultValue = "10") int pageSize)
    {
        System.out.println(category.getIsshow()+"    cid"+"  "+pageNum);
        PageResult pageResult = categoryService.selectCategoryList(category,pageNum,pageSize);
        return ResponseResult.createBySuccess(200,pageResult,"查询成功");
    }
    @PassToken
    @GetMapping("/list1")
//    public ResponseResult list1(Category category){
//        List<Category> list = categoryService.selectCategoryGoodsList(category);
//        return ResponseResult.createBySuccess(200,list,"查询成功");
//    }
    public ResponseResult list1(Category category, @RequestParam(defaultValue = "1") int pageNum,  @RequestParam(defaultValue = "10") int pageSize)
    {
        System.out.println(category.getCid()+"    cid"+"  "+pageNum);
        PageResult pageResult = categoryService.selectCategoryGoodsList(category,pageNum,pageSize);
        return ResponseResult.createBySuccess(200,pageResult,"查询成功");
    }
    /**
     * 导出category列表
     */

//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Category category)
//    {
//        List<Category> list = categoryService.selectCategoryList(category);
//        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
//        util.exportExcel(response, list, "category数据");
//    }

    /**
     * 获取category详细信息
     */

    @GetMapping(value = "/{cid}")
    public ResponseResult getInfo(@PathVariable("cid") Long cid)
    {
        return ResponseResult.createBySuccess(200,categoryService.selectCategoryByCid(cid),"查找成功");
    }

    /**
     * 新增category
     */

    @PostMapping
    public ResponseResult add(@RequestBody Category category)
    {
        int i =categoryService.insertCategory(category);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","添加成功");
        }else{
            return  ResponseResult.createByFail(500,"添加失败");

        }

    }

    /**
     * 修改category
     */

    @PutMapping
    public ResponseResult edit(@RequestBody Category category)
    {
        int i =categoryService.updateCategory(category);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","编辑成功");
        }else{
            return  ResponseResult.createByFail(500,"编辑失败");

        }

    }

    /**
     * 删除category
     */
	@DeleteMapping("/{cids}")
    public ResponseResult remove(@PathVariable Long[] cids)
    { int i =categoryService.deleteCategoryByCids(cids);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","删除成功");
        }else{
            return  ResponseResult.createByFail(500,"删除失败");

        }
    }
}
